#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UTable.Objects;
using UTable.ObjectManager.Commands;
using System.Windows;
using UTable.Input;

namespace UTable.ObjectManager
{
    public class SingleThreadObjectManager : IGlobalObjectManager
    {
        private IObjectTree _objectTree;

        private IInputManager _inputManager;

        public SingleThreadObjectManager()
        {
        }

        private void OnInputDetected(ManagedInputEventArgs args)
        {
            // direct send the inputs to the root object
            (args.Args as UTable.Input.MultiTouch.MultiTouchEventArgs).FingerEvents[0].TimeTrace.GlobalObjectManagerTime = InputEventArgs.GetTimestamp();
            // (args.Args as UTable.Input.MultiTouch.MultiTouchEventArgs).FingerEvents[0].TimeTrace.SetStep(1);

            (ObjectTree.TreeRoot as UObjectEntity).Content.InputManager_InputDetected(args);
        }

        #region IObjectManager Members

        public void StartWork()
        {
            
        }

        public IObjectTree ObjectTree
        {
            get
            {
                return _objectTree;
            }
            set
            {
                this._objectTree = value;
            }
        }

        public IInputManager InputManager
        {
            get
            {
                return _inputManager;
            }
            set
            {
                this._inputManager = value;
                _inputManager.InputDetected += OnInputDetected;
            }
        }

        public delegate void PutCommandsHandler(List<ICommand> cmds);

        public delegate IObject CreateObjectHandler(IObject owner, ObjectCreateParameter parameter);

        public delegate IObject CreateObjectHandler2(ObjectCreateParameter parameter);

        public IObject CreateObject(IObject owner, ObjectCreateParameter parameter)
        {
            UObjectEntity obj = CreateObject(parameter) as UObjectEntity;
            ObjectCommand insertCmd = new ObjectCommand(owner,
                ObjectCommandType.Insert, obj);
            List<ICommand> cmdList = new List<ICommand>();
            cmdList.Add(insertCmd);
            PutCommands(cmdList);
            return obj;
        }

        public IObject CreateObject(ObjectCreateParameter parameter)
        {
            UObjectEntity obj = new UObjectEntity(parameter);
            return obj;
        }


        public void PutCommands(List<ICommand> cmds)
        {
                        // execute commands
            foreach (ICommand cmd in cmds)
            {
                if (cmd.Type == CommandType.SYSTEM)
                {
                    switch ((cmd as SystemCommand).SubType)
                    {
                        case SystemCommandType.Shutdown:
                            Application.Current.Shutdown((int)(cmd as SystemCommand).Param);
                            break;
                    }
                }
            }
        }

        #endregion

        #region IGlobalObjectManager Members


        public System.Windows.Threading.Dispatcher Dispatcher
        {
            get { throw new NotImplementedException(); }
        }

        public void SetLayoutPolicy(IObject obj, Type layoutPolicyType, object param)
        {
            throw new NotImplementedException();
        }

        public void UpdateLayoutPolicy(IObject obj, object param)
        {
            throw new NotImplementedException();
        }

        public void SetAuthorizationPolicy(IObject obj, Type authorizationPolicyType, object param)
        {
            throw new NotImplementedException();
        }

        public void UpdateAuthorizationPolicy(IObject obj, object param)
        {
            throw new NotImplementedException();
        }

        public void SetConflictResolvePolicy(IObject obj, Type conflictResolvePolicyType, object param)
        {
            throw new NotImplementedException();
        }

        public void UpdateConflictResolvePolicy(IObject obj, object param)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
